package com.jash.myutils.utils;

import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jash.myutils.utils.ImageUtils;

/**
 * Created by 阿飞 on 2016/9/29.
 */

public class ViewUtils {

    public static void setTextView(TextView textView, int text) {
        if (textView == null) {
            return;
        }
        setTextView(textView, String.valueOf(text));

    }

    public static void setTextView(TextView textView, String text) {
        if (textView == null || TextUtils.isEmpty(text)) {
            return;
        }
        textView.setText(text);
    }

    public static void setImageView(ImageView imageView, @DrawableRes int drawableResId) {
        if (imageView == null || drawableResId == 0) {
            return;
        }
        imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(drawableResId));
    }

    public static void setImageView(ImageView imageView, String url, @DrawableRes int defaultImageId) {
        if (imageView == null || TextUtils.isEmpty(url) || defaultImageId == 0) {
            return;
        }
        ImageUtils.loadImage(imageView, url, defaultImageId);
    }

    /**
     * 如果 Object 为null,则置为 invisible
     *
     * @param view
     * @param o
     */
    public static void setVisible(View view, Object o) {
        if (view == null) {
            return;
        }
        if (o == null) {
            view.setVisibility(View.INVISIBLE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 如果 visible 数值为 0 ,则置为 invisible
     *
     * @param view
     * @param visible
     */
    public static void setVisible(View view, int visible) {
        if (view == null) {
            return;
        }
        if (visible == 0) {
            view.setVisibility(View.INVISIBLE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

}
